﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.AppCode.gjips;
using MixScript.BLL.MM;
using MixScript.BLL.SD;
namespace MixScriptWeb.Jideco.MM
{
    public partial class fGRN : cCommonWeb2, IMIxCommon
    {
        private static string grnTyp;
        private static DataTable dtGrid;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                mn = (MainMenu2)this.Master;
                hideStatusBar();
                if (!Page.IsPostBack)
                {
                    addFindClickAttribute(this, new MixScriptTB[] { txtSupp }, new string[] { "SRCSUPGJ" });
                    addFindClickAttribute(this, new MixScriptTB[] { txtPONo }, new string[] { "SRCPOSGI" });
                    addRead(new MixScriptTB[] { txtPONo, txtGRN });
                    MultiView1.SetActiveView(vwSelect);
                }            
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #region IMIxCommon Members

        public bool isvalid()
        {
            try
            {

                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            
        }

        #endregion
        private void settodt(GridView grv)
        {
            try
            {
                int c = 0;

                foreach (GridViewRow gv in grv.Rows)
                {
                    TextBox tb = (TextBox)gv.Cells[1].FindControl("txtIORFNO");
                    dtGrid.Rows[c]["iorfno"] = tb.Text;

                    tb = (TextBox)gv.Cells[2].FindControl("txtPartNo");
                    dtGrid.Rows[c]["itno"] = tb.Text;
                    tb = (TextBox)gv.Cells[3].FindControl("txtPartName");
                    dtGrid.Rows[c]["itnm"] = tb.Text;

                    tb = (TextBox)gv.Cells[4].FindControl("txtQty");
                    dtGrid.Rows[c]["ioqt"] = tb.Text;


                    tb = (TextBox)gv.Cells[5].FindControl("txtUom");
                    dtGrid.Rows[c]["unmsns"] = tb.Text;

                    tb = (TextBox)gv.Cells[6].FindControl("txtDesc");
                    dtGrid.Rows[c]["grdesc"] = tb.Text;


                    c += 1;
                }

                dtGrid.AcceptChanges();

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                if (ddlGrnTyp.SelectedIndex > 0)
                {
                    grnTyp = ddlGrnTyp.SelectedValue;
                    MultiView1.SetActiveView(vwGR);

                    dtGrid = new DataTable();
                    dtGrid.Columns.Add("iorfno");
                    dtGrid.Columns.Add("itno");
                    dtGrid.Columns.Add("itnm");
                    dtGrid.Columns.Add("ioqt");
                    dtGrid.Columns.Add("arndqt");
                    dtGrid.Columns.Add("unmsns");
                    dtGrid.Columns.Add("grdesc");



                    DataRow dr = dtGrid.NewRow();
                    dtGrid.Rows.Add(dr);
                    dr = dtGrid.NewRow();
                    dtGrid.Rows.Add(dr);
                    dr = dtGrid.NewRow();
                    dtGrid.Rows.Add(dr);
                    dr = dtGrid.NewRow();
                    dtGrid.Rows.Add(dr);
                    dr = dtGrid.NewRow();
                    dtGrid.Rows.Add(dr);
                    dtGrid.AcceptChanges();

                    gvGRN.DataSource = dtGrid;
                    gvGRN.DataBind();

                }
                else
                {
                    ShowStatusBar("Please Select GRN TYPE");
                }



            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton imb = (ImageButton)sender;
                string s = imb.ClientID;
                //  s = s.Replace("ContentPlaceHolder1_gvPO" + ddlPOType.SelectedValue + "_imgDelete_", "");
                int i = 0;
                int.TryParse(s, out i);
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItemIndex == -1)
                    return;

                Image img = (Image)e.Row.FindControl("imgSearch");
                TextBox tiorfno = (TextBox)e.Row.FindControl("txtIORFNO");
                TextBox tpartno = (TextBox)e.Row.FindControl("txtPartNo");
                TextBox tpartnm = (TextBox)e.Row.FindControl("txtPartName");
                TextBox tuom = (TextBox)e.Row.FindControl("txtUoM");
                TextBox tqty = (TextBox)e.Row.FindControl("txtQty");
                TextBox tdsc = (TextBox)e.Row.FindControl("txtDesc");
                addRead(new TextBox[] { tiorfno, tpartno, tpartnm, tuom, tqty });
                img.Attributes.Add("onclick", "openWndChild('../popupGRN/fm-" + this.Form.ClientID + ",a-" + tpartno.ClientID + ",b-,c-,d-,e-,f-" + "" + ",g-" + grnTyp + ".html',480,640)");
                //img.Attributes.Add("onclick", "openWndChild('../popupGRN/fm-" + this.Form.ClientID + ",a-" + tpartno.ClientID + ",b-,c-,d-,e-,f-" + "" + ",g-" + sdate + ".html',480,640)");

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isvalid())
                {
                    string sout = "";
                    cGRN oGRN = new cGRN();
                    if (!oGRN.isRecordExist("tb_grtrx", new string[] { "grid" }, new string[] { txtGRN.Text }))
                    {
                        //  oMof.sSection = txtSection.Text;
                        settodt(gvGRN);

                        oGRN.sPO = txtPONo.Text;
                        oGRN.sSupp = txtSupp.Text;
                        oGRN.sDelno = txtDONo.Text;
                        oGRN.sReqdept = txtReqDept.Text;
                        if (oGRN.saveGRN(dtGrid, getUserLogin(), out sout))
                        {
                            // txtMOFno.Text = oMof.sMofno;

                            Session["sGRNID"] = oGRN.sGRNno;
                            txtGRN.Text = oGRN.sGRNno;
                            string sscript = "";
                            sscript += "<script>";
                            sscript += "window.open('../print/rptid-7,GRNID-" + "" + ",suppid-" + "" + ",dl.html', '_blank');";
                            //Response.Write("alert('sfdfs');");
                            sscript += "</script>";
                            ClientScript.RegisterStartupScript(GetType(), "report", sscript);
                            //MultiView1.SetActiveView(vwFilter);
                            ShowStatusBar("GRN Created Successfully", msgType.msgSuccess);
                        }
                        else
                        {
                            ShowStatusBar(sout);
                        }

                    }
                    else
                    {
                        ShowStatusBar("PO Number Already Exist");
                    }

                }
                else
                {

                }


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
                //throw
            }
        }
    }
}